<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" id="form">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="userName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">用户ip</label>
                            <div class="layui-input-inline">
                                <input type="text" name="ip" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">开始时间</label>
                            <div class="layui-input-inline">
                                <input type="text" name="startTime" id="startTime" readonly="readonly"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">结束时间</label>
                            <div class="layui-input-inline">
                                <input type="text" name="endTime" id="endTime" readonly="readonly" autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    onclick="$('#form')[0].reset()" lay-filter="data-search-btn"><i
                                    class="layui-icon"></i> 重 置
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <!--表格开始-->
        <table class="layui-hide" id="loginfoTable" lay-filter="loginfoTable"></table>
        <script type="text/html" id="loginfoToolBar">
            <a class="layui-btn layui-btn-sm layui-btn-danger data-count-delete" lay-event="batchdelete">批量删除</a>
        </script>
        <script type="text/html" id="loginfoTableBar">
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>
        <!--表格结束-->

    </div>
</div>
<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../../lib/jq-module/jquery.particleground.min.js" charset="utf-8"></script>
<script src="../../lib/common/jquery.cookie.min.js" charset="utf-8"></script>
<script src="../../lib/common/api.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'laydate', 'layer'], function () {
        const form = layui.form;
        const table = layui.table;
        const laydate = layui.laydate;
        const layer = layui.layer;

        //渲染时间选择器
        laydate.render({
            elem: '#startTime',
            type: 'datetime'
        });
        laydate.render({
            elem: '#endTime',
            type: 'datetime'
        });

        const tableIns = table.render({
            elem: '#loginfoTable',
            url: loginInfo+"/list",
            method:'post',
            contentType: 'application/json',
            toolbar: "#loginfoToolBar",
            cellMinWidth: true,
            cols: [[
                {type: "checkbox", fixed: "left"},
                {field: 'id', title: 'ID', align: "center"},
                {field: 'userId', title: '用户id', align: "center"},
                {field: 'userName', title: '登陆名', align: "center"},
                {field: 'ip', title: '登陆地址', align: "center"},
                {field: 'createTime', title: '登陆时间', align: "center"},
                {title: '操作', templet: '#loginfoTableBar', fixed: "right", align: "center"}
            ]],
            page: true
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function () {
            //执行搜索重载
            console.log($("#form").serializeObject());
            table.reload('loginfoTable', {
                page: {
                    curr: 1
                },
                where:$("#form").serializeObject()
            });
            return false;
        });

        //监听表头的事件
        table.on('toolbar(loginfoTable)', function (obj) {
            var data = obj.data;
            let checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'batchdelete':
                    checkStatus = table.checkStatus('loginfoTable'); //idTest 即为基础参数 id 对应的值
                    var data = checkStatus.data;
                    var length = checkStatus.data.length;
                    if (length === 0) {
                        layer.msg("请选中要删除的行数据");
                    } else {
                        layer.confirm('真的删除选择中这些数据么?', function (index) {
                            $.ajax({
                                url: loginInfo + "/batch",
                                type: 'post',
                                dataType: "json",
                                contentType: "application/json;charset=utf-8",
                                data: JSON.stringify({'delete': data}),
                                success: function (res) {
                                    layer.msg(res.msg);
                                    tableIns.reload();
                                    layer.close(index);
                                },
                                error: function (res) {
                                    alert("登录日志删除错误"+res);
                                }
                            });

                        });
                    }
                    break;
            }
        });

        //监听行数据的事件
        table.on('tool(loginfoTable)', function (obj) {
            const data = obj.data;
            if (obj.event === 'delete') {
                layer.confirm('真的删除这条数据么', function (index) {
                    $.ajax({
                        url: loginInfo + "/" + data.id,
                        type: 'delete',
                        success: function (res) {
                            layer.msg(res.msg);
                            obj.del();
                            layer.close(index);
                        },
                        error: function (res) {
                            alert("登录日志删除错误"+res);
                        }
                    });
                });
            }
        });
    });
</script>
<script>

</script>

</body>
</html>